package com.javaweb.demo.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class EncodingFilter implements javax.servlet.Filter {
    public EncodingFilter() {
        System.out.println("过滤器构造");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String contentType = request.getHeader("Accept");

        String servletPath = request.getServletPath();
        StringBuffer server = request.getRequestURL();
        if (server.toString().contains(".css") || server.toString().contains(".js"))
        {
            filterChain.doFilter(servletRequest, servletResponse);
        }
        else{
            servletRequest.setCharacterEncoding("utf-8"); //将编码改为utf-8
            servletResponse.setContentType(contentType == null ? "text/html;charset=utf-8" : contentType + ";charset=utf-8");
            filterChain.doFilter(servletRequest, servletResponse);
        }

    }

    @Override
    public void destroy() {
        System.out.println("过滤器销毁");
    }

}
